package com.grad.why.studentsystem.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.grad.why.studentsystem.controller.UtilController;
import com.grad.why.studentsystem.domain.Camera;
import com.grad.why.studentsystem.domain.FaceImgPath;
import com.grad.why.studentsystem.domain.Optionparams;
import com.grad.why.studentsystem.domain.current.CurrentFaceImgPath;
import com.grad.why.studentsystem.mapper.FaceImgPathMapper;
import com.grad.why.studentsystem.mapper.OptionparamsMapper;
import com.grad.why.studentsystem.service.ClassesTableService;
import com.grad.why.studentsystem.service.FaceInfoService;
import com.grad.why.studentsystem.service.OptionparamsService;
import com.grad.why.studentsystem.util.Util;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.*;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.List;


@Component
@Service(interfaceClass = FaceInfoService.class)
@Transactional
public class FaceInfoServiceImpl implements FaceInfoService {
    @Autowired
    private FaceImgPathMapper faceImgPathMapper;

    @Autowired
    private OptionparamsMapper optionparamsMapper;

    @Autowired
    private static OptionparamsService optionparamsService;

    @Autowired
    private static FaceInfoService faceInfoService;

    @Override
    public List<CurrentFaceImgPath> getFaceImgPath(int offset, int limit, String search) {
        try{
            return faceImgPathMapper.getFaceImgPath(offset,limit,search);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public long getFaceImgPathLength(String search) {
        try{
            return faceImgPathMapper.getFaceImgPathLength(search);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int addFaceInfoBySidAndFacePath(FaceImgPath faceImgPath) {
        try{
            return faceImgPathMapper.insertSelective(faceImgPath);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Integer getMaxId() {
        try{
            return faceImgPathMapper.getMaxId();
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int deleteFaceInfoByFid(int fid) {
        try{
            FaceImgPath faceImgPath = faceImgPathMapper.selectByPrimaryKey(fid);
            String fname = faceImgPath.getPath();
            String rootPath = optionparamsMapper.getParamValueByParamName("imgRootPath");
            fname = fname.replace("/Path/",rootPath);
            File file = new File(fname);
            file.delete();
            return faceImgPathMapper.deleteByPrimaryKey(fid);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }
}
